module ziyi{
    export class cats{
        private cat:egret.MovieClip;//正常猫
        private weizhucat:egret.MovieClip;//被围住的猫
        //未被围住的猫的movieclip
        private _stayData:any;
        private _stayTexture:egret.Texture;
        //被围住的猫的movieclip
        private _weizhuData:any;
        private _weizhuTexture:egret.Texture;
        public constructor(){
            this.loadstay(this.initMovieClip);
            this.loadweizhu(this.initWeizhClip);
        }
        public getStayCat():egret.MovieClip{
            return this.cat;
        }
        public getWeizhuCat():egret.MovieClip{
            return this.weizhucat;
        }
        private initMovieClip():void {//正常形态的猫的movieclip展示
            var mcDataFactory = new egret.MovieClipDataFactory(this._stayData, this._stayTexture);
            this.cat= new egret.MovieClip(mcDataFactory.generateMovieClipData("stay"));
            this.cat.gotoAndPlay(1,-1);
            this.cat.anchorOffsetX=this.cat.width/2;
            this.cat.anchorOffsetY=this.cat.height/2;
            this.cat.x = 300;
            this.cat.y = 580;
            this.cat.visible=false;
            this.cat.scaleX=1.2;
            this.cat.scaleY=1.2;
        }
        protected loadstay(callback:Function):void {//正常形态的猫的movieclip加载
            var count:number = 0;
            var self = this;
            var check = function () {
                count++;
                if (count == 2) {
                    callback.call(self);
                }
            }
            var loader = new egret.URLLoader();
            loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
                var loader = e.currentTarget;
                this._stayTexture = loader.data;
                check();
            }, this);
            loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
            var request = new egret.URLRequest("resource/stay.png");
            loader.load(request);
            var loader = new egret.URLLoader();
            loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
                var loader = e.currentTarget;
                this._stayData = JSON.parse(loader.data);
                check();
            }, this);
            loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
            var request = new egret.URLRequest("resource/stay.json");
            loader.load(request);
        }
        private initWeizhClip():void {//困住形态的猫的movieclip展示
            var mcDataFactory = new egret.MovieClipDataFactory(this._weizhuData, this._weizhuTexture);
            this.weizhucat= new egret.MovieClip(mcDataFactory.generateMovieClipData("weizhu"));
            //循环展示动作，第二个参数为-1
            this.weizhucat.gotoAndPlay(1,-1);
            this.weizhucat.anchorOffsetX=this.weizhucat.width/2;
            this.weizhucat.anchorOffsetY=this.weizhucat.height/2;
            this.weizhucat.x = 300;
            this.weizhucat.y = 600;
            this.weizhucat.visible=false;
            this.weizhucat.scaleX=1.2;
            this.weizhucat.scaleY=1.2;
        }
        protected loadweizhu(callback:Function):void {//困住形态的猫的movieclip加载
            var count:number = 0;
            var self = this;
            var check = function () {
                count++;
                if (count == 2) {
                    callback.call(self);
                }
            }
            var loader = new egret.URLLoader();
            loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
                var loader = e.currentTarget;
                this._weizhuTexture = loader.data;          
                check();
            }, this);
            loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
            var request = new egret.URLRequest("resource/weizhu.png");
            loader.load(request);       
            var loader = new egret.URLLoader();
            loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
                var loader = e.currentTarget;
                this._weizhuData = JSON.parse(loader.data);
                check();
            }, this);
            loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
            var request = new egret.URLRequest("resource/weizhu.json");
            loader.load(request);
        }
    }
}